import axios from 'axios'

let baseURL = ''
let api = '/api'
// if (process.env.NODE_ENV != 'development') {
    // baseURL = '/api'; // http://172.28.8.21:8282
// }

axios.defaults.baseURL = baseURL
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
axios.defaults.timeout = 1000000000

axios.interceptors.response.use(
    response => {
        let link = window.location.host
        if (response.data.type && response.data.type.indexOf('application') !== -1) {
            return response
        } else if (response.data.type && response.data.type.indexOf('html') !== -1) {
            var reader = new FileReader()
            reader.onload = e => {
                let data = JSON.parse(e.target.result)
                if (data.statusCode === 203) {
                    top.location.href = `http://${link}/user/login/index`
                    return
                } else {
                    alert(data.msg)
                    return
                }
            }
            reader.readAsText(response.data)
        } else if (!response.data.type && response.data.statusCode === 200) {
            return response
        } else if (!response.data.type && response.data.statusCode === 203) {
            top.location.href = `http://${link}/user/login/index`
            return
        } else {
            return response
        }
    },
    error => {
        return Promise.reject(error)
    }
)

export let baseurl = baseURL;
export function get(url) {
    return new Promise(resolve => {
        axios.get(url).then(res => {
            if (!res.data.statusCode || res.data.statusCode === 200) {
                resolve(res.data)
            } else {
                alert(res.data.msg)
            }
        }).catch(error => {
            console.log(error)
        })
    })
}
export function post(url, params) {
    return new Promise(resolve => {
        axios.post(url, params).then(res => {
            // console.log(res);
            if (!res.data.statusCode || res.data.statusCode === 200) {
                resolve(res.data)
            } else {
                alert(res.data.msg)
            }
        }).catch(error => {
            console.log(error)
        })
    })
}
// post2 不提示alert
export function post2(url, params) {
    return new Promise(resolve => {
        axios.post(url, params).then(res => {
            // console.log(res);
            if (!res.data.statusCode || res.data.statusCode === 200) {
                resolve(res.data)
            } else {
                resolve(res.data)
            }
        }).catch(error => {
            console.log(error)
        })
    })
}
// json字符串格式
export function postJSON(url, params) {
    return new Promise(resolve => {
        axios.post(url, JSON.stringify(params)).then(res => {
            // console.log(res);
            if (!res.data.statusCode || res.data.statusCode === 200) {
                resolve(res.data)
            } else {
                alert(res.data.msg)
            }
        }).catch(error => {
            console.log(error)
        })
    })
}

// 获取微信配置文件参数
export const getWxConfig    = params => post(`${axios.defaults.baseURL}${api}/wx/getJsSign`,params)
// 图片上传到微信服务器以后调用这个接口发送serverid
export const getUerInfo     = params => post(`${axios.defaults.baseURL}${api}/wx/getUerInfo`,params)
// 上传发票图片
export const billUpload     = params => post(`${axios.defaults.baseURL}${api}/ocr/billUpload`,params)
// 获取发票识别类别
export const getBillLists   = params => post(`${axios.defaults.baseURL}${api}/ocr/getBillLists`,params)
// 新增发票到发票夹
export const addTicket      = params => post(`${axios.defaults.baseURL}${api}/ocr/addTicket`,params)
// 获取用户信息
export const media          = params => post(`${axios.defaults.baseURL}${api}/ocr/wx/media`,params)
// 获取我的发票列表
export const getListRecord  = params => post(`${axios.defaults.baseURL}${api}/ocr/getListRecord`,params)
// 发票详情
export const getInvoiceInfo = params => post(`${axios.defaults.baseURL}${api}/ocr/getInvoiceInfo`,params)
// 发票图片
export const getInvoiceImage= params => post(`${axios.defaults.baseURL}${api}/ocr/getInvoiceImage`,params)
// 获取发票抬头
export const getListInvoiceHeader= params => post(`${axios.defaults.baseURL}${api}/ocr/getListInvoiceHeader`,params)
// 删除发票
export const delInvoice= params => post(`${axios.defaults.baseURL}${api}/ocr/deleteInvoiceInfo`,params)
// 获取发票状态
export const editCheckInvoiceIsUse= id => get(`${axios.defaults.baseURL}${api}/ocr/editCheckInvoiceIsUse?id=${id}`)


